﻿/*
 * SPSticky.Debug - Facilitates SharePoint sticky notes interaction
 * Version 1.0a
 * @requires jquery-1.7.2.min.js
 * @requires jquery-ui-1.8.21.custom.min.js
 * @requires jquery.jeditable.mini.js
 * @requires jquery.SPServices-0.7.1a.min.js
 *
 * Copyright (c) 2012 Yaroslav Pentsarskyy; www.sharemuch.com
 * Examples and docs at:
 * http://spsticky.codeplex.com
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

function GetUserName()
{
	ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
	var context = null;
	var web = null;
	var currentUser = null;
	getWebUserData();
}
		
function onSuccessMethod(sender, args) {
	var userName = web.get_currentUser().get_title();
	CreateExistingStickys(userName);
}

function onFailureMethod(sender, args) {
} 
		
function getWebUserData() {
		context = new SP.ClientContext.get_current();
		web = context.get_web();
		currentUser = web.get_currentUser();
		currentUser.retrieve();
		context.load(web);
		context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
		Function.createDelegate(this, this.onFailureMethod));
		}
	
function CreateNewItem(stickyText, dialogObj) {
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "New",
        listName: "StickyNotes",
        valuepairs: [["Title", "Sticky"], ["StickyBody", stickyText],
		["VerticalPosition", $(dialogObj).dialog( "option", "position")[0]], 
		["HorizontalPosition", $(dialogObj).dialog( "option", "position")[1]]],
        completefunc: function (xData, Status) {
            $(dialogObj).dialog( "option", "title", "Saved" );
        }
    });
}
		
function DeleteSticky(stickyId)
{
	$().SPServices({
        operation: "UpdateListItems",
		ID: parseInt(stickyId),
        listName: "StickyNotes",
		batchCmd: "Delete",
    });
}
		
function UpdateExistingItem(stickyId, stickyText, dialogObj) {
    $().SPServices({
        operation: "UpdateListItems",
		ID: parseInt(stickyId),
        listName: "StickyNotes",
        valuepairs: [["StickyBody", stickyText], 
		["VerticalPosition", $(dialogObj).dialog( "option", "position")[0]], 
		["HorizontalPosition", $(dialogObj).dialog( "option", "position")[1]]],
        completefunc: function (xData, Status) {
            $(dialogObj).dialog( "option", "title", "Saved" );
        }
    });
}
		
function CreateExistingStickys(currentUser)
{
	$().SPServices({
	operation: "GetListItems",
	async: false,
	listName: "StickyNotes",
	CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Author' /><FieldRef Name='StickyBody' /><FieldRef Name='VerticalPosition' /><FieldRef Name='HorizontalPosition' /></ViewFields>",
	completefunc: function (xData, Status) {
		$(xData.responseXML).SPFilterNode("z:row").each(function() {
		var user = $(this).attr("ows_Author").split("#")[1];
		if (currentUser==user)
		{showExistingSticky($(this).attr("ows_ID"), $(this).attr("ows_StickyBody"), $(this).attr("ows_VerticalPosition"), $(this).attr("ows_HorizontalPosition"));}
		});
	}
	});
}
		
function showExistingSticky(stickyId, StickyBody, VerticalPosition, HorizontalPosition)
{
	$("#dialogs").append("<div id='dialog"+String(stickyId)+"'>"+StickyBody+"</div>");
	var dialogObj = $("#dialog"+String(stickyId)).dialog({
		autoOpen: false,
		width: 200,
		height: 200,
		buttons: {
			"New": function() {
				showSticky();
			}
		}
	});
	$(dialogObj).dialog( "option", "position", [parseInt(VerticalPosition), parseInt(HorizontalPosition)]);
	$("#dialog" +String(stickyId)).dialog({
		close: function(event, ui) { DeleteSticky(stickyId); }
	});
			
	$(dialogObj).editable(function(value, settings) { 
			UpdateExistingItem(stickyId, value, dialogObj);
			return(value);
		}, { 
			type      : 'textarea', 
			onblur    : 'submit', 
			tooltip   : 'Click to edit...', 
		});
	$("#dialog" +String(stickyId)).dialog("open");
}
		
function showSticky()
{
	var randomNum = Math.floor(Math.random() * (100 - 1 + 1)) + 1; 
	$("#dialogs").append("<div id='dialog"+randomNum+"'></div>");
	var dialogObj = $("#dialog"+randomNum).dialog({
		autoOpen: false,
		width: 200,
		height: 200,
		buttons: {
			"New": function() {
				showSticky();
			}
		}
	});
	$(dialogObj).dialog( "option", "position", [100 +randomNum,100 +randomNum]);
	$(dialogObj).editable(function(value, settings) { 
			CreateNewItem(value, dialogObj);
			return(value);
		}, { 
			type      : 'textarea', 
			onblur    : 'submit', 
			tooltip   : 'Click to edit...', 
		});
	$("#dialog" +randomNum).dialog("open");
}